/**
 * 超市管理系统API交互模块
 * 这个文件包含与后端API交互的所有方法
 * 目前是模拟数据，后续可替换为真实API调用
 */

// 模拟API访问延迟
const API_DELAY = 300;

// API基础路径（实际部署时应替换）
const API_BASE_URL = 'api/';

// 模拟数据
const MOCK_DATA = {
    // 库存数据
    inventory: [
        { id: 1, name: '可口可乐', code: 'KK001', category: '饮料', current: 45, safe: 30, status: 'normal' },
        { id: 2, name: '薯片', code: 'SP002', category: '食品', current: 28, safe: 30, status: 'warning' },
        { id: 3, name: '纸巾', code: 'ZJ003', category: '日用品', current: 5, safe: 20, status: 'danger' },
        { id: 4, name: '面包', code: 'MB004', category: '食品', current: 0, safe: 15, status: 'danger' },
        { id: 5, name: '矿泉水', code: 'KQS005', category: '饮料', current: 120, safe: 50, status: 'normal' },
        { id: 6, name: '牙膏', code: 'YG006', category: '日用品', current: 18, safe: 20, status: 'warning' },
        { id: 7, name: '方便面', code: 'FM007', category: '食品', current: 65, safe: 40, status: 'normal' },
        { id: 8, name: '啤酒', code: 'PJ008', category: '饮料', current: 48, safe: 40, status: 'normal' },
    ],
    
    // 分类数据
    categories: [
        { id: 1, name: '食品', count: 142 },
        { id: 2, name: '饮料', count: 89 },
        { id: 3, name: '日用品', count: 64 },
        { id: 4, name: '服装', count: 37 },
        { id: 5, name: '家居', count: 23 },
    ],
    
    // 仪表盘数据
    dashboard: {
        todaySales: 12580.50,
        orderCount: 15,
        lowStockCount: 8,
        memberCount: 246
    }
};

// API类
const API = {
    /**
     * 获取库存数据
     * @param {Object} filters - 筛选条件
     * @returns {Promise} - 返回库存数据的Promise
     */
    getInventory: function(filters = {}) {
        return new Promise((resolve) => {
            setTimeout(() => {
                let data = [...MOCK_DATA.inventory];
                
                // 应用筛选条件
                if (filters.category && filters.category !== 'all') {
                    data = data.filter(item => item.category === filters.category);
                }
                
                if (filters.status && filters.status !== 'all') {
                    data = data.filter(item => item.status === filters.status);
                }
                
                if (filters.search) {
                    const searchTerm = filters.search.toLowerCase();
                    data = data.filter(item => 
                        item.name.toLowerCase().includes(searchTerm) || 
                        item.code.toLowerCase().includes(searchTerm)
                    );
                }
                
                resolve({
                    success: true,
                    data: data,
                    total: data.length,
                    warning: data.filter(item => item.status === 'warning').length,
                    danger: data.filter(item => item.status === 'danger').length
                });
            }, API_DELAY);
        });
    },
    
    /**
     * 获取仪表盘数据
     * @returns {Promise} - 返回仪表盘数据的Promise
     */
    getDashboardData: function() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve({
                    success: true,
                    data: MOCK_DATA.dashboard
                });
            }, API_DELAY);
        });
    },
    
    /**
     * 获取商品分类数据
     * @returns {Promise} - 返回分类数据的Promise
     */
    getCategories: function() {
        return new Promise((resolve) => {
            setTimeout(() => {
                resolve({
                    success: true,
                    data: MOCK_DATA.categories
                });
            }, API_DELAY);
        });
    },
    
    /**
     * 更新商品库存信息
     * @param {Object} product - 商品信息
     * @returns {Promise} - 返回更新结果的Promise
     */
    updateInventory: function(product) {
        return new Promise((resolve) => {
            setTimeout(() => {
                // 模拟更新逻辑
                const index = MOCK_DATA.inventory.findIndex(item => item.id === product.id);
                if (index !== -1) {
                    MOCK_DATA.inventory[index] = {...MOCK_DATA.inventory[index], ...product};
                    
                    // 更新状态
                    if (MOCK_DATA.inventory[index].current === 0) {
                        MOCK_DATA.inventory[index].status = 'danger';
                    } else if (MOCK_DATA.inventory[index].current < MOCK_DATA.inventory[index].safe) {
                        MOCK_DATA.inventory[index].status = 'warning';
                    } else {
                        MOCK_DATA.inventory[index].status = 'normal';
                    }
                    
                    resolve({
                        success: true,
                        message: '商品库存信息更新成功',
                        data: MOCK_DATA.inventory[index]
                    });
                } else {
                    resolve({
                        success: false,
                        message: '商品不存在',
                        data: null
                    });
                }
            }, API_DELAY);
        });
    }
}; 